﻿using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhouyu.DreamTour.WinService.AuditPassSMS;
using ZhouYu.DreamTour.DAL.IRepository;
using ZhouYu.DreamTour.Model;
using ZhouYu.DreamTour.ViewModel;
using ZhouYu.DreamTour.Web.Models;
using ZhouYu.DreamTour.WinService;

namespace Zhouyu.DreamTour.WinService.Job
{
    public class TouristRouteJob : IJob
    {
        private IOrderRepository _orderRepository;
        private IPaymentRecordRepository _paymentRecordRepository;
        private IUserRepository _userRepository;
        public TouristRouteJob()
        {
            _orderRepository = AutofacUtil.GetFromFac<IOrderRepository>();
            _paymentRecordRepository = AutofacUtil.GetFromFac<IPaymentRecordRepository>();
            _userRepository = AutofacUtil.GetFromFac<IUserRepository>();
        }


        public async Task Execute(IJobExecutionContext context)
        {
            string Name = "";

            ResponseResult responseResult = new ResponseResult();
            TenXunYunSmSForTouristRoute tenXunYunSmSForTouristRoute = new TenXunYunSmSForTouristRoute();
            try
            {
                //查询未通知状态的支付记录
                List<PaymentRecord> paymentRecords = _paymentRecordRepository.SelPaymentRecordInfo();
                //循环查询支付进行指定操作
                foreach (var item in paymentRecords)
                {
                    ////修改订单状态
                    //responseResult.Success = _orderRepository.UpdateOrderStatus(item.Payment_OrderNo);
                    //根据关联的用户表和支付记录表查询用户号码与用户名
                    UserInfo userInfo = _userRepository.SelUserInfoById(item.User_Id);
                    var strTime = item.Payment_OutingTime.ToString();
                    var dt = DateTime.Parse(strTime);
                    var month = dt.ToString("MM");
                    var day = dt.ToString("dd");
                    Name = item.Payment_OrderName;
                    Name = Name.Substring(1, 4);
                    // File.AppendAllText("D:\\Log.txt", UserInfoContext.userInfoContext.UserInfo.User_Telphone.ToString() + UserInfoContext.userInfoContext.UserInfo.User_Name + month + day + item.Payment_OrderName);
                    //发送短信
                    tenXunYunSmSForTouristRoute.SetSMS(userInfo.User_Telphone.ToString(), userInfo.User_Name, month, day, Name);
                    //短信通知之后，将该条支付记录状态修改为以通知
                    responseResult.Success = _paymentRecordRepository.UpdatePaymentStatus(1, item.Payment_OrderNo);
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText("D:\\Ex.txt", ex.ToString());
                throw;
            }
            //支付操作
            await Task.Delay(1);
        }
    }
}
